<extend name="Layout/page_popup" />
<block name="content">
	<div class="panel panel-default ">
		<div class="panel-heading clearfix">
			<div class="pull-left">
				<label>
					<input class="ace"  type="radio" id="rb_dept" name="type" value="dept" >
					<span class="lbl">部门</span> </label>

				<label class="display-none">
					<input  class="ace"  type="radio" id="rb_rank" name="type" value="rank">
					<span class="lbl">职级</span> </label>

				<label>
					<input  class="ace"  type="radio" id="rb_position" name="type" value="position">
					<span class="lbl">职位</span> </label>
			</div>
			<div class="pull-right">
				<a  onclick="save();" class="btn btn-sm btn-primary">确定</a>
				<a  onclick="myclose();" class="btn btn-sm btn-primary">关闭</a>
			</div>
		</div>
		<div class="panel-body">
			<div class="col-28 pull-left">
				<div class="">
					<b>地址簿</b>
				</div>
				<div class="popup_tree_menu" >
					<div id="dept" class="display-none" style="height:200px;">
						{$list_dept}
					</div>
					<div id="rank" class="display-none" style="height:200px;">
						{$list_rank}
					</div>
					<div id="position" class="display-none" style="height:200px;">
						{$list_position}
					</div>
				</div>
				<div>
					<div id="addr_list" style="width:100%;height:210px;"></div>
				</div>
			</div>
			<div class="col-34 pull-right">
				<div>
					<b style="padding-left: 60px;">管理</b><span id="rc_count"></span>
				</div>
				<div class="clearfix" style="margin-bottom: 15px;">
					<label class="col-4 pull-left text-right" ><a onclick="add_address('rc');" class="btn btn-sm btn-primary"> <i class="fa fa-angle-double-right"></i> </a> </label>
					<div class="col-28 pull-right">
						<div id="rc" style="width:100%;height:130px;overflow:hidden">
							<select size="6" style="height:100%;width:100%;"></select>
						</div>
					</div>
				</div>

				<div>
					<b  style="padding-left: 60px;">写入\修改</b><span id="cc_count"></span>
				</div>
				<div class="clearfix" style="margin-bottom: 15px;">
					<label class="col-4 pull-left text-right" ><a onclick="add_address('cc');" class="btn btn-sm btn-primary"> <i class="fa fa-angle-double-right"></i></a></label>
					<div class="col-28 pull-right">
						<div id="cc" style="width:100%;height:130px;overflow:hidden">
							<select size="6" style="height:100%;width:100%;"></select>
						</div>
					</div>
				</div>
				<div>
					<b  style="padding-left: 60px;">读取</b><span id="cc_count"></span>
				</div>
				<div class="clearfix">
					<label class="col-4 pull-left text-right" ><a onclick="add_address('bcc');" class="btn btn-sm btn-primary"> <i class="fa fa-angle-double-right"></i></a></label>
					<div class="col-28 pull-right">
						<div id="bcc" style="width:100%;height:95px;overflow:hidden">
							<select size="6" style="height:100%;width:100%;"></select>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>
</block>
<block name="js">
	<script type="text/javascript">
		function save() {
			$("#rc select option").each(function(i) {				
				emp_no = $(this).val();
				name = jQuery.trim($(this).text()).replace(/<.*>/, '');
				html = conv_inputbox_item(name, emp_no);
				$("#admin_list .address_list", parent.document).append(html);
			});

			$("#cc select option").each(function(i) {
				emp_no = $(this).val();
				name = jQuery.trim($(this).text()).replace(/<.*>/, '');
				html = conv_inputbox_item(name, emp_no);
				$("#write_list .address_list", parent.document).append(html);
			});

			$("#bcc select option").each(function(i) {				
				emp_no = $(this).val();
				name = jQuery.trim($(this).text()).replace(/<.*>/, '');
				html = conv_inputbox_item(name, emp_no);
				$("#read_list .address_list", parent.document).append(html);
			});
			myclose();
		}

		function showdata(result) {
			$("#addr_list").html("");
			if ( type = $("input[name='type']:checked").val() == "dept") {
				var dept_id = "dept_" + $("#dept a.active").attr("node");
				var dept_name = $("#dept a.active span").text();
				var name = dept_name + "&lt;dept@group&gt;";
				var html_string = conv_address_item(name,dept_id);
				$("#addr_list").html(html_string);
			}
			for (s in result.data) {
				var id = result.data[s].id;
				var emp_no = result.data[s].emp_no;	
				var position_name = result.data[s].position_name;							
				var name = result.data[s].name;
				var name = name + "/" + position_name;
				var html_string = conv_address_item(name,emp_no);
				$("#addr_list").append(html_string);
			}
		}


		$(document).ready(function() {

			$("#rb_{$type}").prop('checked', true);
			// 选择用户默认选择的类型
			$("#{$type}").removeClass("display-none");

			$("input[name='type']").on('click', function() {
				$("input[name='type']").each(function() {
					$("#" + $(this).val()).addClass("display-none");
				});
				$("#" + $(this).val()).removeClass("display-none");
			});
			//点击节点时读取相应的数据
			$(".tree_menu  a").click(function() {
				$(".tree_menu a").removeClass("active");
				var type = $("input[name='type']:checked").val();
				$(this).addClass("active");
				sendAjax("{:U('read')}", "type=" + type + "&id=" + $(this).attr("node"), function(data) {
					showdata(data);
				});
				return false;
				//禁止连接生效
			});

			// 双击添加到收件人 因后添加的数据 所以需要用live函数
			$(document).on("dblclick", "#addr_list label", function() {
				$text = $(this).text();
				$val = $(this).find("input").val();
				if ($("#rc select option[value='" + $val + "']").val() == undefined) {
					$("<option></option>").val($val).text($text).appendTo("#rc select");
					$("#rc_count").text("(" + $("#rc select option").length + ")");
				};
			});

			/* 双击添加到收件人 因后添加的数据 所以需要用live函数 */
			$("#rc select").on("dblclick", function() {
				$(this).find("option:selected").remove();
				$("#rc_count").text("(" + $("#rc select option").length + ")");
			});

			$("#cc select").on("dblclick", function() {
				$(this).find("option:selected").remove();
				$("#cc_count").text("(" + $("#cc select option").length + ")");
			});

			$("#bcc select").on("dblclick", function() {
				$(this).find("option:selected").remove();
				$("#bcc_count").text("(" + $("#bcc select option").length + ")");
			});			
		});
		//最终确认

		function add_address(name) {
			$("input:checked[name='addr_id']").each(function() {
				$(this).prop('checked',false);
				$text = $(this).parents("label").find("span").text();
				$val = $(this).val();
				if ($("#" + name + " select option[value='" + $val + "']").val() == undefined) {
					$("<option></option>").val($val).text($text).appendTo("#" + name + " select");
					$("#" + name + "_count").text("(" + $("#" + name + " select option").length + ")");
				};
			});
		}
		//-->
	</script>
</block>
